package com.zixuan.gulicollege.service_order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zixuan.gulicollege.common_utils.ConstantConfigInfo;
import com.zixuan.gulicollege.common_utils.OrderNumberUtils;
import com.zixuan.gulicollege.entity.eduservice.vo.CourseInitFrontVO;
import com.zixuan.gulicollege.entity.order.Order;
import com.zixuan.gulicollege.entity.ucenter.UcenterMember;
import com.zixuan.gulicollege.service_order.client.EduCourseClient;
import com.zixuan.gulicollege.service_order.mapper.OrderMapper;
import com.zixuan.gulicollege.service_order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zixuan
 * @since 2021-05-26
 */
@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduCourseClient eduCourseClient;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public String createOrder(String courseId, UcenterMember userInfo) {
        // 获取课程信息
        CourseInitFrontVO courseInfo = eduCourseClient.getCourseInfo(courseId);

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", userInfo.getId());
        Order order = baseMapper.selectOne(wrapper);
        if (order != null) {
            long oldTime = order.getGmtCreate().getTime();
            long newTime = System.currentTimeMillis();
            if ((newTime - oldTime) < ConstantConfigInfo.THIRTY_MINUTES_MS_VALUE) {
                return order.getOrderNo();
            } else {
                baseMapper.deleteById(order);
            }
        }
        // 生成订单号
        String orderNum = OrderNumberUtils.getInstance().GenerateOrder();

        order = new Order();
        // 创建order对象 存储数据
        order.setOrderNo(orderNum);
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());
        order.setMemberId(userInfo.getId());
        order.setNickname(userInfo.getNickname());
        order.setMobile(userInfo.getMobile());

        baseMapper.insert(order);
        // 返回订单号
        return orderNum;
    }

    @Override
    public Order getOrderInfoByOrderOn(String orderOn) {
        QueryWrapper<Order> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("order_no", orderOn);
        return baseMapper.selectOne(orderWrapper);
    }

    @Override
    public boolean getOrderBuyByUser(String userId, String courseId) {
        // 通过用户id查询订单信息
        if (userId == null || Objects.equals(userId, "")) {
            return false;
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", userId);
        wrapper.eq("course_id", courseId);
        Order order = baseMapper.selectOne(wrapper);
        if (order == null) {
            return false;
        }
        return order.getStatus() != 0;
    }

    @Override
    public boolean updateOrderStatus(Map<String, String> params) {
        String orderNum = params.get("out_trade_no");
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNum);
        Order order = new Order();
        order.setStatus(1);
        return baseMapper.update(order, wrapper) >= 0;
    }
}
